/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.phai.srtfix.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Formatter;

/**
 *
 * @author beehind
 */
public class Fix {

    File fileInput;
    File fileOutput;
    boolean majuSelected;
    Integer hour;
    Integer minute;
    Double second;
    int no;

    public Fix(File fileInput, File fileOutput, boolean majuSelected, Integer hour, Integer minute, Double second) {
        no = 1;
        this.fileInput = fileInput;
        this.fileOutput = fileOutput;
        this.majuSelected = majuSelected;
        this.hour = hour;
        this.minute = minute;
        this.second = second;

    }

    public boolean fixSrt() {
        try {
            fileOutput.createNewFile();

            BufferedReader br = new BufferedReader(new FileReader(fileInput));
            BufferedWriter bw = new BufferedWriter(new FileWriter(fileOutput));


            String fix;

            String input = br.readLine();
            while (input != null) {
                fix = input;
                if (input.matches("\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d\\s-->\\s\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d")) {
                    fix = processInput(input);

                }

                bw.write(fix + "\r\n");

                input = br.readLine();
            }
            br.close();
            bw.close();

        } catch (Exception ex) {
            ex.printStackTrace();
            return false;

        }
        System.out.println("nyampe...");
        return true;
    }

    private String processInput(String input) throws Exception {

        String fix;

        String[] time = input.split("\\s-->\\s");

        System.out.println(no++);

        fix = calculateTime(time[0]) + " --> " + calculateTime(time[1]);
        return fix;
    }

    private String calculateTime(String time) throws Exception {

        String[] string = time.split(":");

        Integer h = Integer.parseInt(string[0]);
        Integer m = Integer.parseInt(string[1]);
        Double s = Double.parseDouble(string[2].replace(',', '.'));


        Integer fh;
        int fh_tambah;

        Integer fm;
        int fm_tambah;

        Double fs;

        if (majuSelected) {
            fs = (second + s) % 60;
            fm_tambah = new Double((second + s) / 60).intValue();

            fm = ((minute + m) + fm_tambah) % 60;
            fh_tambah = ((minute + m) + fm_tambah) / 60;

            fh = ((hour + h) + fh_tambah) % 24;



        } else {
            Double tot = s + (m * 60) + (h * 60 * 60);
            Double hoh = second + (minute * 60) + (hour * 60 * 60);

            Double ftot = tot - hoh;


            fs = ftot % 60;
            fm = new Double((ftot / 60) % 60).intValue();
            fh = new Double((ftot / 3600) % 24).intValue();

            if (fs < 0 || fm < 0 || fh < 0) {
                throw new Exception("Hour, Minute, Second terlalu besar");
            }

        }

        String _h = ((fh.toString().length() != 2) ? new String("0" + fh) : fh.toString());
        String _m = ((fm.toString().length() != 2) ? new String("0" + fm) : fm.toString());

        StringBuilder sb = new StringBuilder();
        // Send all output to the Appendable object sb
        Formatter formatter = new Formatter(sb);


        String _stmp = formatter.format("%1$.3f", fs).toString();

       
        String _s = ((_stmp.length() != 6) ? new String("0" + _stmp) : _stmp).replace('.', ',');

        String hasil = _h  + ":" + _m + ":" + _s;

        

        System.out.println(hasil);

        return hasil;
    }
}
